<?php
namespace frontend\controllers;

use common\models\ImageTypeModel;
use common\models\LianTypeModel;
use common\models\LianWebModel;
use common\models\NovelModel;
use common\models\NovelTypeModel;
use common\models\SetAuthorModel;
use frontend\models\SmsCodeModel;
use Yii;
use yii\data\Pagination; //分页
use frontend\base\BaseController;
use frontend\utils\GetUrl;
use frontend\utils\UrlUtil;
use frontend\utils\CurlUtil;

/**
 * Xiashu controller
 */
class XiashuController extends BaseController
{
    public $name = '';
    public $type = '';
    public static $novel_type='';

    //下书网  小说类型
    public function actionGetType()
    {
        $str = file_get_contents('http://www.xiabook.com/');
        $up = '/<ul.*class="cl">(.*)<\/ul>/Uis'; 
        preg_match_all($up, $str, $a);
        $par = '/<li.*><a.*href="(.*)".*>(.*)<\/a><\/li>/Uis';
        preg_match_all($par, implode('',$a[1]), $arr);
        $res = [];
        if(!empty($arr)){
            unset($arr[0]);
            $num = count($arr[1]);
            foreach ($arr as $key => $value) {
                foreach ($value as $k => $val) {
                    if($key == 1){
                        $name = 'url';
                    }elseif($key == 2){
                        $name = 'title';
                    }
                    if($k == 2 && $key ==2){
                        $val = '世界名著';
                    }
                    $res[$k][$name] = trim(str_replace('"','',$val));
                }
            }
        }
        if(!empty($res)){
            foreach ($res as $k => $val) {
                $data[] = ['title' => $val['title'], 'source' => 2, 'url' => $val['url'],'time'=>time()];
            }
        }
        $model = NovelTypeModel::tableName();
        $attr = ['title', 'source', 'url','time'];
        $flag = Yii::$app->db->createCommand()->batchInsert($model, $attr, $data)->execute();
        echo $flag;
    }

    public function actionGetNovel()
    {
        ini_set("max_execution_time", "72000");
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
        $url = 'http://www.xiabook.com';
        $type_list = NovelTypeModel::find()->select(['id','url','title','page'])->where(['source'=>2])->asArray()->all();
        if(!empty($type_list)){
            $flag = true;
            foreach ($type_list as $k => $val) {
                if($flag){
                    for ($i=$val['page'];;$i++){
                        $url_str = 'index_'.$i.'.html';
                        if($i == 1){
                            $url_str = '';
                        }
                        $str = @file_get_contents($val['url'].$url_str);
                        if(!$str){
                            break;
                        }
                        $par = '/<div.*class="bbox">.*<div.*class="bpic"><img src="(.*)".*width=80.*height=112.*class="img1".*alt=".*"><\/div>.*<div.*class="bintro">.*<h3><a.*href="(.*)".*target="_blank">(.*)<\/a><span>作者：<a.*href=".*".*target="_blank".*title="(.*)">.*<\/a><\/span><\/h3>.*<div class="ex">.*<p>最新章节.*<\/p>.*<p>(.*)<\/p>.*<\/div>/Uis';
                        preg_match_all($par, $str, $arr);
                        if(!empty($arr)){
                            $stas = NovelTypeModel::updateAll(['page'=>$i+1],['id'=>$val['id']]);
                            unset($arr[0]);
                            $data = $res = [];
                            foreach ($arr as $key => $value) {
                                foreach ($value as $num => $v) {
                                    if($key == 1){
                                        $name = 'image';
                                    }elseif($key == 2){
                                        $name = 'url';
                                        $v = $url.$v;
                                    }elseif($key == 3){
                                        $name = 'title';
                                    }elseif($key == 4){
                                        $name = 'author';
                                    }elseif($key == 5){
                                        $name = 'desc';
                                    }
                                    $res[$num][$name] = trim(str_replace('"','',$v));
                                }
                            }
                            if(!empty($res)){
                                foreach ($res as $x => $y) {
                                    $count = NovelModel::find()->where(['title' => $y['title'],'source'=>2,'type'=>$val['id']])->count();
                                    if($count == 0) {
                                        $data[] = ['title' => $y['title'], 'source' => 2, 'url' => $y['url'], 'author'=>$y['author'], 'type'=>$val['id'], 'desc'=>$y['desc'], 'image'=>$y['image']];
                                    }
                                }
                                if(!empty($data)){
                                    $model = NovelModel::tableName();
                                    $attr = ['title', 'source', 'url','author','type','desc','image'];
                                    $status = Yii::$app->db->createCommand()->batchInsert($model, $attr, $data)->execute();
                                    // sleep(5);
                                    echo $status;
                                }
                            }
                        }else{
                            break;
                        }
                    }
                }else{
                    continue;
                }
            }
        }
    }

}